package com.hongyuan.sellfitness.ui.find.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.find.beans.CircleBeans;
import com.hongyuan.sellfitness.ui.person.view_mode.FansItemViewModel;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class ChoseCircleViewModel extends CustomViewModel<MyRepository> {

    private boolean isLeft = true;

    public ChoseCircleViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<ChoseCricleLeftItemViewModel> leftList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<ChoseCricleLeftItemViewModel> leftBinding = ItemBinding.of(BR.viewModel, R.layout.item_chose_circle_left);

    //给RecyclerView添加ObservableList
    public ObservableList<ChoseCricleRightItemViewModel> rightList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<ChoseCricleRightItemViewModel> rightBinding = ItemBinding.of(BR.viewModel, R.layout.item_chose_circle_right);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //弹框提示
        public SingleLiveEvent<Void> showCancel = new SingleLiveEvent<>();
    }



    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(isLeft && (code == Constants.GET_CIRCLE_CATEGORY_LIST)){
            List<CircleBeans.ListBean> mList = ((CircleBeans)dataBean).getList();

            for(int i = 0 ; i < mList.size() ; i++){
                if(i == 0){
                    mList.get(0).setSelect(true);
                }

                ChoseCricleLeftItemViewModel itemViewModel = new ChoseCricleLeftItemViewModel(this,mList.get(i));
                if(i == 0){
                    this.oldViewModel = itemViewModel;
                }
                leftList.add(itemViewModel);
            }

            isLeft = false;

            //请求右边数据
            clearParams().setParams("parent_id",String.valueOf(mList.get(0).getCategory_id()));
            requestData(Constants.GET_CIRCLE_CATEGORY_LIST);

        }else{

            if(clickViewModel != null){
                oldViewModel.entity.get().setSelect(false);
                oldViewModel.entity.notifyChange();

                clickViewModel.entity.get().setSelect(true);
                clickViewModel.entity.notifyChange();

                oldViewModel = clickViewModel;
            }



            List<CircleBeans.ListBean> mList = ((CircleBeans)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                rightList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(CircleBeans.ListBean bean : mList){
                    ChoseCricleRightItemViewModel itemViewModel = new ChoseCricleRightItemViewModel(this,bean);
                    rightList.add(itemViewModel);
                }
            }

            if(rightList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

    }

    //当前需要关注的对象
    public ChoseCricleLeftItemViewModel oldViewModel;
    public ChoseCricleLeftItemViewModel clickViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);
        this.clickViewModel = (ChoseCricleLeftItemViewModel) itemVM;
    }
}
